<?php

class FeedService
{
    private $storageAdapter;
    
    public function __construct()
    {
        $this->storageAdapter = new FeedStorageAdapter();
    }
    
    public function getFeedByName($name)
    {
        return $this->storageAdapter->getFeed($name);
    }
    
    /**
     * Adds the specified keyword-feed
     *
     * @param string $name
     * @throws AlreadyExistsException if the keyword already exists
     * @throws NullException if the name is empty
     * @return boolean always true
     */
    public function addFeed($name,$data=array())
    {
        if(!$name)
        {
            throw new NullException("No name specified");
        }
        if($this->hasFeed($name))
        {
            throw new AlreadyExistException("Feed '$name' already exists");
        }
        
        return $this->storageAdapter->createFeed($name,$data);
    }
    
    /**
     * Returns the storage adapter
     * 
     * @return IFeedStorageAdapter
     */
    public function getStorageAdapter()
    {
        return $this->storageAdapter;
    }

    /**
     * Sets the storage adapter
     *
     * @param IFeedStorageAdapter $storageAdapter
     */
    public function setStorageAdapter(IFeedStorageAdapter $storageAdapter)
    {
        $this->storageAdapter = $storageAdapter;
        return true;
    }
    
    public function hasFeed($name)
    {
        return $this->storageAdapter->hasFeed($name);
    }
    
    public function listFeeds()
    {
        $feeds = array();
        try
        {
            $feeds = $this->storageAdapter->getFeeds();
        }
        catch(NotFoundException $e)
        {
        }
        return $feeds;

    }
    
    public function deleteFeed($name)
    {
        if(!$this->hasFeed($name))
        {
            throw new NotFoundException("Feed '$name' does not exist");
        }
        
        return $this->storageAdapter->deleteFeed($name);
    }
}
